06. Wrestling with the Swift Compiler
We’ve mentioned the importance of the Swift compiler for creating apps that will run on our device. We've also mentioned that the Swift compiler can be a little difficult to work with at times. We are going to see this first-hand now. Let’s go back to the ControlCenter.swift file, and look at the code inside moveSimpleRobot.
The code inside moveSimpleRobot is responsible for moving the robot about the maze.
Notice that we’ve told the robot to move up. The syntax we’ve used for this is robot.moveUp(). But what happens if you accidentally forget how to write this instruction and instead write robot.goUp()?
Does the Swift compiler understand what it means for the robot to "go up"?
If you try to build and run the project now, it fails! Instead, the Swift compiler and Xcode present some warning signs. As you write code, you’ll have to remember that the Swift compiler only understands a very limited set of vocabulary. This would be like if a musician was given a sheet of music with symbols they’d never seen before. When we give the compiler a new instruction that it doesn’t understand, it presents us with errors. If we change this back to the original, you’ll notice that the warnings go away.
Try the following:
- Produce a Swift compiler error by writing an instruction the robot did not understand (i.e.
robot.goUp). - Correct the error by changing the line of code back to
robot.moveUp().
We’ll explore more Swift syntax and errors later. For now, let’s start thinking about how we can help our robot get from its original location to the star.
Helping the Robot
Prepare
Let’s stay out of Xcode for a second and make sure we can help solve the robot’s problem. We'll do this by writing an algorithm. An algorithm describes the steps we need to take to solve a problem. Right now, we know that the robot, the maze, and the star are positioned like this when the app starts:
By default, the robot is centered in the maze and the star is to the left.
We will write our algorithm using pseudocode. Pseudocode is not real code, but is more like temporary notes or scratch work. Writing the pseudocode ahead of time will allow us to see any errors in our problem solution before we go through the trouble of writing actual code. The bigger the problem, the more time we can save by doing this.
Typically, writing algorithms in pseudocode can be done free-form (in other words, you can write whatever you want that assists you in solving the problem). But, for this exercise, try to write your pseudocode solution using only the following items:
- moveUp
- moveDown
- moveLeft
- moveRight
Here is an example algorithm. Note: It does not actually solve the problem, so technically this is an incorrect algorithm.
This algorithm has two steps: moveRight followed by moveUp.
Learn
QUESTION:
Now it is your job to write your algorithm. Using only moveUp, moveDown, moveLeft, and moveRight, write a sequence of steps that helps the robot reach the star!
ANSWER:
Great job! We’ll see if you’re right in a bit.
Reflect
QUESTION:
Do you think there is more than one algorithm that gets the robot from the starting position to the star?
ANSWER:
Absolutely! There is more than one path for the robot to take to reach the star, after all.
It’s Rarely One Size Fits All
To get the robot to the star, you could have taken any number of approaches. The algorithm below was not the most direct approach:
Using this algorithm, the robot reaches the star, but could we use fewer steps?
The important takeaway here is that there is more than one way to solve this problem. In fact, this is usually the case for problems we solve with code. For nearly any given problem, there are a limitless number of ways we can solve it. So does that mean there is a best way? Well… that question is not as straightforward as you might imagine. Generally speaking, it's best to aim for elegant, optimal solutions to the problems that you are given.
But what does it mean for an algorithm to be elegant and optimal? Here are a few ways to describe an elegant, optimal solution:
- An algorithm that is simple to write and describe.
- An algorithm that completes quickly (relative to the size of the problem).
- An algorithm that uses a small amount of computer memory.
Reflect
QUESTION:
Right now, these considerations are very high-level given all we are doing is helping a robot get to a star. But imagine we were writing code to help the robot find the star when we have no idea where the star will be. How might you approach it?
ANSWER:
Whatever your algorithm, whatever your solution, the takeaway here is that your solution may not always match my solution or a colleague’s solution. Over time, you’ll learn how to write the best solutions—the ones that are the most elegant and the most optimal.